package com.elin4it.blog.controller.manage;

import com.elin4it.blog.pojo.Article;
import com.elin4it.blog.pojo.ArticleCustom;
import com.elin4it.blog.pojo.ArticleType;
import com.elin4it.blog.service.ArticleService;
import com.elin4it.blog.service.ArticleTypeService;
import com.elin4it.blog.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.List;

/**
 * User:Elin
 * Date:2015/8/31
 * Description:
 */
@Controller
@RequestMapping("/blogManage")
public class BlogManageController {

    @Autowired
    private ConfigService configService;
    @Autowired
    private ArticleTypeService articleTypeService;
    @Autowired
    private ArticleService articleService;

    private final String flag = "manage";

    @ModelAttribute
    public void commonMessage(ModelMap map) throws Exception {
        //得到标题和副标题
        String title = configService.findTitle();
        String subTitle = configService.findSubTitle();

        //把标题和副标题放入到model中
        map.put("title", title);
        map.put("subtitle", subTitle);
        //设置页面标识
        map.put("flag", flag);
    }

    @RequestMapping("/addBlog")
    public ModelAndView addBlog(@RequestParam(value = "id", required = false) Integer id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("blog/addBlog");
        if (id != null) {
            ArticleCustom article = articleService.findArticle(id);
            modelAndView.addObject("article", article);
        }
        List<ArticleType> articleTypes = articleTypeService.findArticleTypeList();
        modelAndView.addObject("articleTypes", articleTypes);

        return modelAndView;
    }

    @RequestMapping("/addBlogHandle")
    public String addBlogHandle(Article article, RedirectAttributes redirectAttributes) throws Exception {

        //把上传时间设置为当前
        article.setSubmitTime(new Date());
        int id;
        if (article.getId() != null) {
            //修改操作
            articleService.updateArticle(article);
            id = article.getId();
        } else {
            //调用service方法添加博客，返回文章id
            id = articleService.addArticle(article);
        }

        //把得到的id作为到下一个页面
        redirectAttributes.addAttribute("id", id);
        return "redirect:/blog/blog";
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteBlog")
    public String deleteBlogHandle(Integer id) throws Exception {
        articleService.deleteArticle(id);
        return "redirect:/blog/allBlog";
    }
}
